package com.skyhub.controller;

import com.skyhub.common.domain.dto.HeatMap;
import com.skyhub.common.domain.pojo.*;
import com.skyhub.service.HomeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@RestController
@RequestMapping("/home")
@RequiredArgsConstructor
@CrossOrigin
public class HomeController {

    private final HomeService homeService;
    @GetMapping("/articles")
    public Result index(){
        List<Article>articles=homeService.ArticleList();
        return Result.success("数据加载成功",articles);
    }

    @GetMapping("/getUserNameBySeriesId/{seriesId}")
    public Result getUserNameBySeriesId(@PathVariable("seriesId") Integer seriesId){
        User user=homeService.getUserNameByseriesId(seriesId);
        return Result.success("数据加载成功",user);
    }

    @GetMapping("/getArticleById/{id}")
    public Result GetArticleById(@PathVariable("id")Integer id){

        Article article=homeService.getArticleById(id);

        return Result.success("数据加载成功",article);
    }

    @GetMapping("/getArticleBySeriesId/{seriesId}")
    public Result GetArticleBySeriesId(@PathVariable("seriesId")Integer seriesId){
        List<Article> articles=homeService.getArticleBySeriesId(seriesId);

        return Result.success("数据加载成功",articles);
    }

    @GetMapping("/systemMessageList/{id}")
    public Result systemMessageList(@PathVariable("id")Integer id){
        List<SystemMessage> message=homeService.systemMessageList(id);

        return Result.success("数据加载成功",message);
    }

    @GetMapping("/getArticleSeriesById/{id}")
    public Result getArticleSeriesById(@PathVariable("id")Integer id){
        ArticleSeries articles=homeService.getArticleSeriesById(id);

        return Result.success("数据加载成功",articles);
    }

    @GetMapping("/getArticleSeriesListByUserId/{userId}")
    public Result getArticleSeriesListByUserId(@PathVariable("userId")Integer userId){
        List<ArticleSeries> articles=homeService.getArticleSeriesListByUserId(userId);
        return Result.success("数据加载成功",articles);
    }

    @DeleteMapping("/DeleteArticleById/{id}/{userId}")
    public Result DeleteArticleById(@PathVariable("id")Integer id,@PathVariable("userId") Integer userId){

        homeService.DeleteArticleById(id,userId);

        return Result.success("删除成功","文章删除成功");
    }

    @DeleteMapping("/DeleteArticleSeriesById/{id}/{userId}")//删除文章系列
    public Result DeleteArticleSeriesById(@PathVariable("id")Integer id,@PathVariable("userId") Integer userId){

        homeService.DeleteArticleSeriesById(id,userId);

        return Result.success("删除成功","文章系列删除成功");
    }

    @GetMapping("/GetAllArticle/{userId}")//获取用户所有文章
    public Result GetAllArticle(@PathVariable("userId")Integer userId){
        List<Article> articles=homeService.GetAllArticle(userId);

        return Result.success("数据加载成功",articles);
    }

    @GetMapping("/parse/{userId}/{seriesId}")
    public Result parse(@PathVariable("userId")Integer userId,@PathVariable("seriesId")Integer seriesId){
        Map<String,Object>map=new HashMap<>();

        String message=homeService.parseCard(userId,seriesId);

        String[] split=message.split("-");

        map.put("column",split[0]);

        map.put("cate",split[1]);

        return Result.success("数据加载成功",map);
    }

    @GetMapping("/latestArticles/{userId}")
    public Result latestArticles(@PathVariable("userId")Integer userId){
        List<Article> articles=homeService.latestArticles(userId);

        return Result.success("数据加载成功",articles);
    }

    @GetMapping("/heatMap/{userId}")
    public Result heatMap(@PathVariable("userId")Integer userId){
        List<HeatMap> heatMaps=homeService.heatMap(userId);

        return Result.success("数据加载成功",heatMaps);
    }

    @PostMapping("/addAccessCount/{articleId}")
    public Result addAccessCount(@PathVariable("articleId")Integer articleId){
        homeService.addAccessCount(articleId);
        return Result.success("数据加载成功","文章访问次数增加成功");
    }

    @PutMapping("/thumbsUp/{articleId}/{userId}")
    public Result thumbsUp(@PathVariable("articleId")Integer articleId,@PathVariable("userId")Integer userId){

        homeService.ThumbsUpTheArticle(articleId,userId);

        return Result.success("点赞成功","个人页中可查看");
    }

    @DeleteMapping("/removeThumbsUp/{articleId}/{userId}")
    public Result removeThumbsUp(@PathVariable("articleId")Integer articleId,@PathVariable("userId")Integer userId){

        homeService.removeThumbsUpTheArticle(articleId,userId);

        return Result.success("取消点赞成功","个人页中可查看");
    }

    @GetMapping("/getRecords/{userId}")
    public Result getRecords(@PathVariable("userId")Integer userId){

        List<Article> records = homeService.getRecords(userId);

        return Result.success("获取成功",records);
    }

    @GetMapping("/hotsArticle")
    public Result hotsArticle(){
        List<Article> articles=homeService.hotsArticle();
        return Result.success("数据加载成功",articles);
    }
}
